<template>
  <div class="table-box">
    <div class="operation-box">
      <div class="input-box">
        角色名称：
        <el-input
          v-model="search"
          placeholder="请输入关键字"
          class="input__item"
        />
        <el-button type="danger">查询</el-button>
      </div>

      <div class="add-box">
        <el-button type="primary" @click="openAddDialog">
          <el-icon><CirclePlus /></el-icon>
          添加角色
        </el-button>
      </div>
    </div>

    <el-table
      :data="tableData"
      stripe
      border
      table-layout="auto"
      style="max-width: 80vw"
    >
      <el-table-column prop="id" label="角色编号" />
      <el-table-column prop="name" label="角色名称" />
      <el-table-column prop="createName" label="创建人" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column prop="state" label="状态（启用/未启用）">
        <template #default> 是 </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500"
      align-center
    >
      <el-form label-width="auto" :model="dialogInfo" style="width: 400px">
        <el-form-item label="角色编号">
          <el-input v-model="dialogInfo.id" />
        </el-form-item>
        <el-form-item label="角色名称">
          <el-input v-model="dialogInfo.name" />
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="dialogInfo.state">
            <el-radio border :value="true">启用</el-radio>
            <el-radio border :value="false">不启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            确认
          </el-button>
          <el-button type="primary" @click="dialogVisible = false">
            取消
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { ref } from "vue";

interface User {
  id: string;
  name: string;
  state: boolean;
}

const dialogTitle = ref<"添加用户" | "修改用户">();
const search = ref("");
const dialogVisible = ref(false);

const dialogInfo = ref({
  id: "",
  name: "",
  state: false,
});

const tableData = [
  {
    id: "ps_role01",
    name: "系统管理员",
    createName: "admin",
    updateTime: "2013-12-23 13:24:14",
    state: true,
  },
  {
    id: "ps_role02",
    name: "线路管理员",
    createName: "admin",
    updateTime: "2013-12-23 13:24:14",
    state: false,
  },
  {
    id: "ps_role03",
    name: "线路管理员",
    createName: "admin",
    updateTime: "2013-12-23 13:24:14",
    state: false,
  },
  {
    id: "ps_role04",
    name: "线路管理员",
    createName: "admin",
    updateTime: "2013-12-23 13:24:14",
    state: false,
  },
];

const openAddDialog = () => {
  dialogVisible.value = true;
  dialogTitle.value = "添加用户";
  // 清除原有表格数据
  dialogInfo.value = {
    id: "",
    name: "",
    state: false,
  };
};

const handleEdit = (row: User) => {
  dialogTitle.value = "修改用户";
  dialogInfo.value.id = row.id;
  dialogInfo.value.name = row.name;
  dialogInfo.value.state = row.state;
  dialogVisible.value = true;
};

const handleDelete = (index: number) => {
  console.log(index);
  ElMessage({
    message: "Congrats, this is a success message.",
    type: "success",
  });
};
</script>

<style scoped>
.table-box {
  width: 1000px;
  margin: 0 auto;
}

.operation-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.input-box {
  color: #747070;
  font-size: 14px;
}

.input__item {
  width: 250px;
  margin: 10px;
}
</style>
